<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 9/28/14
 * Time: 12:38 PM
 */

namespace log;

if (!defined('NYMPH_ROOT')) {
  define('NYMPH_ROOT', dirname("../../../" . __FILE__));
  require(NYMPH_ROOT . '/local/loader/kernel/classes/URLClassLoader.php');
}

use parser\XMLParser;

class XMLParserImpl extends XMLParser
{

  private $tmpFiles;

  function __construct($tmpFiles)
  {
    $this->tmpFiles = $tmpFiles;
  }

  /**
   * (PHP 5)<br/>
   * Trying to parse some object
   * @param string $object
   * @return array with created logger instance
   */
  function parse($object)
  {
    $resultArray = null;
    $xmlFile = simplexml_load_file($object);
    foreach ($xmlFile->logger as $logger) {
      $tmpLogger = null;
      $logConfig = new LogConfig();
      $fileLogger = false;
      switch ($logger['type']) {
        case LoggerFactory::BROWSER_LOGGER_TYPE:
          $tmpLogger = new BrowserLogger();
          break;
        case LoggerFactory::FILE_LOGGER_TYPE:
          $tmpLogger = new FileLogger();
          $fileLogger = true;
          break;
      }
      if ($tmpLogger != null) {
        $logConfig->setLogLevel($logger->config->logLevel);
        $logConfig->setName($logger['name']);
        $logConfig->setPattern($logger->config->pattern);
        $logger->config->pathToFile;
        if ($fileLogger) {
          $append = false;
          if ($logger['append'] == "true") {
            $append = true;
          }
          $logConfig->setAppend($append);
          $logConfig->setPathToFile($logger->config->pathToFile);
        }
        $tmpLogger->setLogConfig($logConfig);
        $resultArray[] = $tmpLogger;
      }
    }
    return $resultArray;
  }

  function init()
  {
    $this->addFilesToParse($this->tmpFiles);
  }

}